<template>
	<view class="container">
		<u-tabs
			class="tabsList"
			:list="tabsList" 
			:is-scroll="false" 
			:current="current" 
			@change="change"
			:bold="true"   
			font-size="30"
			bar-width="100" 
			active-color="#2979ff"
			inactive-color="#000000" 
		></u-tabs>
		
		<view class="lost-found-list">
			<view class="item" v-for="(item, index) in lostFoundList" :key="index" @click="goLostFoundDetail(item.foundId)">
				<image mode="aspectFill" :src="item.pic ? item.pic : '/static/images/icon/no-pic.png'"></image>
				<view class="text">
					<view class="title">
						<u-tag v-if="item.type == 1" text="招领" mode="dark" type="success"/>
						<u-tag v-if="item.type == 2" text="寻物" mode="dark" type="error"/>
						{{item.title}}
					</view>
					<view class="content">{{item.content}}</view>
					<view class="handle">
						<u-button v-if="current != 3" size="mini" shape="circle" @click="edit(item.foundId)">编辑</u-button>
						<u-button v-if="current != 3" size="mini" shape="circle" @click="del(item.foundId)">删除</u-button>
					</view>
				</view>
			</view>
			

			<u-empty v-if="lostFoundList.length == 0" mode="list"></u-empty>
			
			<u-toast ref="uToast" />
			
		</view>
		
	</view>
</template>

<script>
	import {
		getMyLostFoundList,deleteByFoundId
	} from '@/api/lostFound.js'
	export default {
		data() {
			return {
				tabsList: [
					{
						name: '发布中'
					},{
						name: '我的招领'
					},{
						name: '我的寻物'
					},{
						name: '已解决'
					}],
				current: 0,
				lostFoundList: []
				
			}
		},
		methods: {
			change(index) {
				this.current = index;
				this.lostFoundList = []
				this.getMyLostFoundList()
			},
			getMyLostFoundList() {
				let params = {}
				if (this.current == 0) {
					params.status = 0
				} else if (this.current == 1) {
					params.status = 0
					params.type = 1
				} else if (this.current == 2) {
					params.status = 0
					params.type = 2
				} else if (this.current == 3) {
					params.resolved = 1
				} 
				getMyLostFoundList(params).then(res => {
					this.lostFoundList = res.data
				})
			},
			// 导航到详情页
			goLostFoundDetail(foundId) {
				this.$u.route('/pages/lost-found-detail/lost-found-detail', {
					foundId: foundId
				})
			},
			// 点击修改
			edit(foundId) {
				this.$u.route('/pages/add-lost-found/add-lost-found', {
					foundId: foundId
				});
			},
			// 确认删除
			del(foundId) {
				uni.showModal({
					title: '温馨提示',
					content: "确定删除吗？",
					success: (res) => {
						if (res.confirm) {
							deleteByFoundId(foundId).then(res => {
								this.getMyLostFoundList()
								this.$refs.uToast.show({
									title: '已删除',
									type: 'success'
								})
							})
						}
					}
				})
				
			}
			
		},
		onLoad() {
			this.getMyLostFoundList()
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.getMyLostFoundList();
			uni.stopPullDownRefresh();
		}
	}
</script>

<style lang="scss">
	@import "./my-lost-found.scss";
</style>
